Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS: Use objc2 crates #188

Merged
merged 2 commits into from
May 5, 2024
Merged

macOS: Use objc2 crates #188

merged 2 commits into from
May 5, 2024

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Apr 24, 2024

These replace the objc family of crates, and provide a bunch of improvements on top of it.

Improvements relevant for rfd:

  • No longer need to call msg_send!, (almost) all methods have automatically generated bindings in objc2-foundation.
  • MainThreadMarker, allows marking explicitly the functions that must run on the main thread.
  • Blocks in block2 have correct memory management, you no longer need to call mem::forget.
  • AppKit bindings are provided in objc2-app-kit, you no longer need to define NSApplication, NSAlertStyle and such manually.

To do this change, I had to change how AsModal works, as it was previously relying on the fact that NSAlert and NSSavePanel just so happened to have mostly compatible methods; now we ensure statically that the correct method is called.

Please do ask if there's anything about either the crates or the code here that's unclear!

Comment on lines 12 to 15
pub fn activate_cocoa_multithreading() {
unsafe {
let thread: *mut Object = msg_send![class!(NSThread), new];
let _: () = msg_send![thread, start];
}
let thread = NSThread::new();
unsafe { thread.start() };
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of yet another improvement: This function was previously leaking, as the NSThread wasn't released at the end of the scope. objc2 instead handles this kind of memory management for you automatically.

These replace the `objc` family of crates, and provide a bunch of
improvements on top of it.

Improvements relevant for `rfd`:
- No longer need to call `msg_send!`, (almost) all methods have
  automatically generated bindings in `objc2-foundation`.
- `MainThreadMarker`, allows marking explicitly the functions that must
  run on the main thread.
- Blocks in `block2` have correct memory management, you no longer need
  to call `mem::forget`.
- AppKit bindings are provided in `objc2-app-kit`, you no longer need to
  define `NSApplication`, `NSAlertStyle` and such manually.

To do this change, I had to change how `AsModal` works, as it was
previously relying on the fact that `NSAlert` and `NSSavePanel` just so
happened to have mostly compatible methods; now we ensure statically
that the correct method is called.
macos-latest is now macos-14, which runs on aarch64
Copy link
Owner

@PolyMeilex PolyMeilex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thank you!

@PolyMeilex PolyMeilex merged commit c1b2c85 into PolyMeilex:master May 5, 2024
13 checks passed
@madsmtm madsmtm deleted the objc2 branch May 5, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants